package com.shenxuchen.crm.web.controller;

import com.shenxuchen.crm.pojo.Type;
import com.shenxuchen.crm.pojo.Value;
import com.shenxuchen.crm.service.TypeService;
import com.shenxuchen.crm.service.ValueService;
import com.shenxuchen.crm.utils.UUIDUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("value")
public class ValueController {
    @Resource
    private ValueService valueService;

    @Resource
    private TypeService typeService;

    @GetMapping("getHasRef")
    @ResponseBody
    public String[] getHasRef(String[] codes) {
        return valueService.getHasRef(codes);
    }

    @GetMapping("index.html")
    public ModelAndView index() {
        List<Value> values = valueService.getAllWithType();
        ModelAndView mv = new ModelAndView();
        mv.addObject("values", values);
        mv.setViewName("dictionary/value/index");
        return mv;
    }

    @GetMapping("save.html")
    public ModelAndView save() {
        // 查询字典类型，加载下拉框
        List<Type> types = typeService.getAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("types", types);
        mv.setViewName("dictionary/value/save");
        return mv;
    }

    @PostMapping("save")
    public String save(Value value) {
        value.setId(UUIDUtils.getUUID());
        valueService.save(value);
        return "redirect:index.html";
    }

    @GetMapping("edit.html")
    public ModelAndView edit(String id) {
        // 查询字典值信息，用于回显数据
        Value value = valueService.getByIdWithType(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("value", value);
        mv.setViewName("dictionary/value/edit");
        return mv;
    }

    @PostMapping("edit")
    public String edit(Value value) {
        valueService.edit(value);
        return "redirect:index.html";
    }

    @GetMapping("delete")
    public String delete(String[] ids) {
        valueService.delete(ids);
        return "redirect:index.html";
    }

    @GetMapping("dragChangeOrderNo")
    public void dragChangeOrderNo(String[] ids) {
        valueService.dragChangeOrderNo(ids);
    }

    @GetMapping("loadByCode")
    @ResponseBody
    public List loadByCode(String code) {
        return valueService.loadByCode(code);
    }
}
